<?php
class VariablesLibrary extends AppModel
{
  var $name = 'VariablesLibrary';
  var $useTable = "variables";
  var $id = "v_id";
  var $primaryKey = "v_id";

  function get_variables($v_vt_id)
  {
    $cond = "v_vt_id=$v_vt_id";
		$rows = $this->findAll($cond, "v_id, v_name, v_desc, v_vt_id, v_user_id", "v_name", null, 1, null);
		return $rows;
	}

	function get_variables_with_default($vt_id, $default = 1)
	{
    //and vbt_is_default=$default
		$where = (!empty($vt_id))?"and v_vt_id = $vt_id":"";
		//$q = "select v_id, v_name, v_vt_id, vbt_bt_id, vbt_is_default, vbt_value from wf_variables, wf_variables_basic_types ";
		//$q .= "where vbt_v_id = v_id $where order by v_vt_id, v_name";

    $q = "select v_id, v_name, v_vt_id, vbt_bt_id, vbt_is_default, vbt_value from wf_variables, wf_variables_basic_types v1 ";
    $q .= "where vbt_v_id = v_id and (vbt_is_default=1 or (vbt_is_default=0 and ";
    $q .= "(select count(*) from wf_variables_basic_types v2 where v2.vbt_v_id = v1.vbt_v_id) = 1)) order by v_vt_id, v_name";
		$rows = $this->query($q);
		return $rows;
	}

	function getFieldValue($field, $key, $key_value)
	{
		$rows = $this->field($field, array("$key" => "$key_value"));
		return $rows;
  }

  function getFieldsValue($whereArray, $fields)
  {
		$rows = $this->find($whereArray, $fields, null, null);
		return $rows;
  }

  function setPrimaryKey ($key)
  {
    $this->primaryKey = $key;
  }

  function getListVariables()
  {
    return $this->generateList(null, "v_name", null, "{n}.VariablesLibrary.v_id", "{n}.VariablesLibrary.v_name");
  }

}
?>
